package com.zmj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.dto.TableData;
import com.zmj.entity.SysRole;
import com.zmj.mapper.SysRoleMapper;
import com.zmj.utils.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    public TableData findRoleAll(String search, String order, int offset, int limit) {
        PageHelper.offsetPage(offset,limit);
        List<SysRole> list = sysRoleMapper.findRoleAll(search,order);
        PageInfo pageInfo = new PageInfo(list);
        TableData tableData = new TableData();
        tableData.setTotal(pageInfo.getTotal());
        tableData.setRows(pageInfo.getList());
        return tableData;
    }


    public Response saveRole(SysRole sysRole) {
        int i = sysRoleMapper.insertSelective(sysRole);
        return i>0?Response.success():Response.error("新增失败");
    }


    public Response roleInfo(long roleId) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
        return Response.success().put("role",sysRole);
    }


    public Response updata(SysRole sysRole) {
        int i = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        return i>0?Response.success():Response.error("修改失败");
    }


    public Response del(List<Long> ids) {
        int i = sysRoleMapper.del(ids);
        return i>0?Response.success():Response.error("删除失败");
    }
    public Response userNotHasRole(long userId) {
        List<SysRole> list = sysRoleMapper.findUserNotHasRole(userId);
        return Response.success().put("roles",list);
    }

    public Response insertUserRole(long userId,long roleId){
        int i=sysRoleMapper.insertUserRole(userId,roleId);
        return i>0?Response.success():Response.error();
    }
}
